package com.devshephard.springcloud.controller;


import com.devshephard.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod") //配置默认的服务降级
public class PaymentController {

    @Resource
    PaymentService paymentService;

    @GetMapping("/payment/feign/hystrix/ok/{id}")
    @ResponseBody
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        return paymentService.paymentInfo_OK(id);
    }

    @GetMapping("/payment/feign/hystrix/timeout/{id}")
    @ResponseBody
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        return paymentService.paymentInfo_TimeOut(id);
    }

    @GetMapping("/payment/hystrixCommond/timeout/{id}")
    @ResponseBody
    public String paymentInfoHystrixCommond_TimeOut(@PathVariable("id") Integer id) {
        return paymentService.paymentInfoHystrixCommond_TimeOut(id);
    }

    @GetMapping("/payment/hystrixCommond/error/{id}")
    @ResponseBody
    public String paymentInfoHystrixCommond_Error(@PathVariable("id") Integer id) {
        return paymentService.paymentInfoHystrixCommond_Error(id);
    }

    //消费端自己定义服务降级
    @GetMapping("/consumer/payment/hystrix/orderTimeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")  //3秒钟以内就是正常的业务逻辑
    })
    public String paymentInfo_OrderTimeOut(@PathVariable("id") Integer id){
        String result = paymentService.paymentInfo_TimeOut(id);
        return result;
    }

    //消费端自己定义默认的服务降级
    @GetMapping("/consumer/payment/hystrix/OrderDefaultHystrix/{id}")
    @HystrixCommand
    public String paymentInfo_OrderDefaultHystrix(@PathVariable("id") Integer id){
        String result = paymentService.paymentInfo_TimeOut(id);
        return result;
    }





    //兜底方法
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
        return "我是消费者80，对付支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,(┬＿┬)";
    }

    //下面是全局fallback方法
    public String payment_Global_FallbackMethod(){
        return "GlobalDefault异常处理信息，请稍后再试,(┬＿┬)";
    }

}
